﻿using DFGK.IService;
using DFGK.Repository;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.物料管理
{
    /// <summary>
    /// 物料分类
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class MaterialCategoryController : BaseController
    {
        private readonly IMaterialCategoryService materialCategoryService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="materialCategoryService"></param>
        public MaterialCategoryController(IWebHostEnvironment environment, IMaterialCategoryService materialCategoryService) : base(environment)
        {
            this.materialCategoryService = materialCategoryService;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddMaterialCategory(MaterialCategory m)
        {
            if (string.IsNullOrEmpty(m.Code))
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            string message = "";
            bool IsOK = materialCategoryService.AddMaterialCategory(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialCategortList(string? mname = "", int pageIndex = 1, int pageSize = 3)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = materialCategoryService.GetMaterialCategortList(mname, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败", "500", 0));
        }

        /// <summary>
        /// 添加-树形显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialCateMenuitemList()
        {
            var list = materialCategoryService.GetMaterialCateMenuitemList();
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "获取成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        } 

        /// <summary>
        /// 根据Id获取对象信息
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMaterialCategoryById(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var rule = materialCategoryService.GetMaterialCategoryById(mid);
            if (rule != null)
            {
                return Ok(BaseResult.Result(rule, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！Id可能有误或已被删除！", "500"));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdMaterialCategory(MaterialCategory m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.Code))
                return Ok(BaseResult.Result("400", "编码不能为空！"));       
            string message = "";
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            bool IsOK = materialCategoryService.UpdMaterialCategory(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMaterialCategory(long mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = materialCategoryService.DelMaterialCategory(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！Id可能有误或已被删除！", "500"));
        }
    }
}
